
const WebSocketPlugin = {

  install (Vue, options) {
    const clientId = Math.random().toString(36).substr(2)
    let ws = ''

    if ('WebSocket' in window) {
      // 连接WebSocket节点
      ws = new WebSocket(options.url + clientId)
      // 将WebSocket实例挂载到Vue原型，方便全局访问
      Vue.prototype.$websocket = ws
    } else {
      alert('Not support websocket')
    }

    // 监听WebSocket的打开事件
    ws.addEventListener('open', function (event) {
      console.log('WebSocket is connected.', event.message)
    })

    // 监听WebSocket的消息事件
    ws.addEventListener('message', function (event) {
      // console.log('Message from WebSocket:', event.data)
    })

    // 监听WebSocket的关闭事件
    ws.addEventListener('close', function (event) {
      console.log('WebSocket is closed now.', event.data)
    })

    // 监听WebSocket的错误事件
    ws.addEventListener('error', function (event) {
      console.error('WebSocket error observed:', event.message)
    })

    // 发送消息
    ws.addEventListener('send', function (event) {
      ws.send('message')
      console.error('WebSocket error observed:', event.message)
    })

    /*  ws.sendMseeage((message) => {
      ws.sendMseeage(message)
    }) */
  }

}

export default WebSocketPlugin
